﻿using AuthCenter.API.Application.Command.Request;
using AuthCenter.Domain.Request;
using AuthCenter.ErrorCode;
using AuthCenter.Intertructrue.Interface;
using MediatR;

namespace AuthCenter.API.Application.Handler.Request
{
    public class DelRequestHandler : IRequestHandler<DelRequestCommand, APIResult<int>>
    {
        private readonly IBaseRepository<RequestModel> requestRepo;

        public DelRequestHandler(IBaseRepository<RequestModel> requestRepo)
        {
            this.requestRepo = requestRepo;
        }
        public Task<APIResult<int>> Handle(DelRequestCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var res = requestRepo.GetAll().FirstOrDefault(x=>x.Id == request.Id);
            if (res == null)
            {
                result.Message = "找不到此编号";
                result.Code = APIEnum.Fail;
                return Task.FromResult(result);
            }
            res.IsDel = true;
            int r = requestRepo.UpdateAsync(res).Result;
            if (r > 0)
            {
                result.Message = "删除申请成功";
                result.Code = APIEnum.Ok;
                result.Data = r;
            }
            else
            {
                result.Message = "删除申请失败";
                result.Code = APIEnum.Fail;
                result.Data = r;
            }
            return Task.FromResult(result);
        }
    }
}
